package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.base.service.IIpLogService;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private ILoginInfoService loginInfoService;
    @Autowired
    private IIpLogService ipLogService;

    @RequestMapping("userlogin")
    public JsonResult login(LoginInfo loginInfo) {
        JsonResult jsonResult = new JsonResult();
        Integer state = IpLog.STATE_FAILURE;
        try {
            loginInfoService.login(loginInfo, LoginInfo.USERTYPE_MGR);
            state = IpLog.STATE_SUCCESS;
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }

        //保存登录日志
        ipLogService.save(loginInfo.getUsername(), state, IpLog.USERTYPE_MGR);
        return jsonResult;
    }

}
